package encryptmethod

import (
	"os"
	"math"
	"crypto/md5"
	"io"
	"encoding/hex"
)
const FILECHUNK  = 8192

func LargeFilemd5(theFilepath string) (themd5 string,err error) {
	file,err := os.Open(theFilepath)
	if err != nil{
		return
	}
	defer file.Close()
	info,err := file.Stat()
	if err != nil{
		return
	}
	filesize := info.Size()
	blocks := uint64(math.Ceil(float64(filesize)/float64(FILECHUNK)))
	hash := md5.New()
	for i := uint64(0);i <blocks;i++{
		blocksize := int(math.Min(FILECHUNK,float64(filesize-int64(i*FILECHUNK))))
		buf := make([]byte,blocksize)
		file.Read(buf)
		io.WriteString(hash,string(buf))
	}
	themd5 = hex.EncodeToString(hash.Sum(nil))
	return
}